home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / rdflib / events.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  3.7 KB  |  105 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """
  5. Dirt Simple Events
  6.  
  7. A Dispatcher (or a subclass of Dispatcher) stores event handlers that
  8. are 'fired' simple event objects when interesting things happen.
  9.  
  10. Create a dispatcher:
  11.  
  12.   >>> d = Dispatcher()
  13.  
  14. Now create a handler for the event and subscribe it to the dispatcher
  15. to handle Event events.  A handler is a simple function or method that
  16. accepts the event as an argument:
  17.  
  18.   >>> def handler1(event): print `event`
  19.   >>> d.subscribe(Event, handler1)
  20.  
  21. Now dispatch a new event into the dispatcher, and see handler1 get
  22. fired:
  23.  
  24.   >>> d.dispatch(Event(foo='bar', data='yours', used_by='the event handlers'))
  25.   <rdflib.events.Event ['data', 'foo', 'used_by']>
  26. """
  27.  
  28. class Event(object):
  29.     '''
  30.     An event is a container for attributes.  The source of an event
  31.     creates this object, or a subclass, gives it any kind of data that
  32.     the events handlers need to handle the event, and then calls
  33.     notify(event).
  34.  
  35.     The target of an event registers a function to handle the event it
  36.     is interested with subscribe().  When a sources calls
  37.     notify(event), each subscriber to that even will be called i no
  38.     particular order.
  39.     '''
  40.     
  41.     def __init__(self, **kw):
  42.         self.__dict__.update(kw)
  43.  
  44.     
  45.     def __repr__(self):
  46.         attrs = self.__dict__.keys()
  47.         attrs.sort()
  48.         return [] % ([ a for a in attrs ],)
  49.  
  50.  
  51.  
  52. class Dispatcher(object):
  53.     '''
  54.     An object that can dispatch events to a privately managed group of
  55.     subscribers.
  56.     '''
  57.     _dispatch_map = None
  58.     
  59.     def set_map(self, amap):
  60.         self._dispatch_map = amap
  61.  
  62.     
  63.     def get_map(self):
  64.         return self._dispatch_map
  65.  
  66.     
  67.     def subscribe(self, event_type, handler):
  68.         ''' Subscribe the given handler to an event_type.  Handlers
  69.         are called in the order they are subscribed.
  70.         '''
  71.         if self._dispatch_map is None:
  72.             self.set_map({ })
  73.         
  74.         lst = self._dispatch_map.get(event_type, None)
  75.         if lst is None:
  76.             lst = [
  77.                 handler]
  78.         else:
  79.             lst.append(handler)
  80.         self._dispatch_map[event_type] = lst
  81.  
  82.     
  83.     def dispatch(self, event):
  84.         """ Dispatch the given event to the subscribed handlers for
  85.         the event's type"""
  86.         if self._dispatch_map is not None:
  87.             lst = self._dispatch_map.get(type(event), None)
  88.             if lst is None:
  89.                 raise ValueError('unknown event type: %s' % type(event))
  90.             lst is None
  91.             for l in lst:
  92.                 l(event)
  93.             
  94.         
  95.  
  96.  
  97.  
  98. def test():
  99.     import doctest
  100.     doctest.testmod()
  101.  
  102. if __name__ == '__main__':
  103.     test()
  104.  
  105.